package work.mediway.mdm.controller.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mediway.hos.base.model.BaseResponse;
import com.mediway.hos.log.annotation.OperLog;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.core.annotation.OpsLog;
import work.mediway.mdm.core.enums.CodeAndMessageEnum;
import work.mediway.mdm.core.enums.OpsLogTypeEnum;
import work.mediway.mdm.biz.dto.biz.SsFieldPermDTO;
import work.mediway.mdm.biz.dto.biz.SsTablePermDTO;
import work.mediway.mdm.biz.entity.biz.SsUserLinkField;
import work.mediway.mdm.biz.entity.biz.SsUserLinkTable;
import work.mediway.mdm.biz.service.biz.SsUserLinkFieldService;
import work.mediway.mdm.biz.service.biz.SsUserLinkTableService;
import work.mediway.mdm.biz.vo.biz.SsFieldPermVO;
import work.mediway.mdm.biz.vo.biz.SsTablePermVO;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2020/11/27
 */
@Api(tags = "字段表权限配置模块")
@ApiSupport(order = 7)
@Validated
@RequestMapping("perm")
@RestController
public class FieldTablePermController {

    @Resource
    private SsUserLinkTableService userLinkTableService;
    @Resource
    private SsUserLinkFieldService userLinkFieldService;


    @OperLog(title = "保存或者更新表权限信息", content = "保存或者更新表权限信息")
    @OpsLog(value = "保存或者更新表权限信息", type = OpsLogTypeEnum.INSERT)
    @PostMapping("table")
    @ApiOperation(value = "保存或者更新表权限信息(传入ssLinkId为更新)")
    @ApiImplicitParam(name = "fieldTablePermSaveDTO", value = "SsFieldTablePermSaveDTO集合(形如[{},{}])")
    @ApiOperationSupport(order = 1)
    public BaseResponse<String> saveTablePerm(@RequestBody @Valid List<SsUserLinkTable> userLinkTables) {
        this.userLinkTableService.saveTablePerm(userLinkTables);
        return BaseResponse.success();
    }

    @OperLog(title = "保存或者更新字段权限信息", content = "保存或者更新字段权限信息")
    @OpsLog(value = "保存或者更新字段权限信息", type = OpsLogTypeEnum.INSERT)
    @PostMapping("field")
    @ApiOperation(value = "保存或者更新字段权限信息(传入ssLinkId为更新)")
    @ApiImplicitParam(name = "fieldTablePermSaveDTO", value = "SsFieldTablePermSaveDTO集合(形如[{},{}])")
    @ApiOperationSupport(order = 3)
    public BaseResponse<String> saveFieldPerm(@RequestBody @Valid List<SsUserLinkField> userLinkFields) {
        this.userLinkFieldService.saveFieldPerm(userLinkFields);
        return BaseResponse.success();
    }

    @OpsLog(value = "查询表权限信息", type = OpsLogTypeEnum.SELECT)
    @GetMapping("table")
    @ApiOperation(value = "查询表权限信息")
    @ApiOperationSupport(order = 5)
    public BaseResponse<Page<SsTablePermVO>> listTablePerm(SsTablePermDTO tablePermDTO, QueryRequest queryRequest) {
        Page<SsTablePermVO> page = this.userLinkTableService.pageTablePerm(tablePermDTO, queryRequest);
        return ObjectUtil.isNotEmpty(page) ? BaseResponse.success(page) : BaseResponse.error();
    }

    @OpsLog(value = "查询字段权限信息", type = OpsLogTypeEnum.SELECT)
    @GetMapping("field")
    @ApiOperation(value = "查询字段权限信息")
    @ApiOperationSupport(order = 7)
    public BaseResponse<List<SsFieldPermVO>> listFieldPerm(@Valid SsFieldPermDTO fieldPermDTO) {
        List<SsFieldPermVO> result = this.userLinkFieldService.listFieldPerm(fieldPermDTO);
        return CollUtil.isNotEmpty(result) ? BaseResponse.success(result) : BaseResponse.error(CodeAndMessageEnum.DB_RESOURCE_NULL.getMessage());
    }

}
